<?php

class SuperMod extends UsersBase
{
	public $verifyPassword;
	public $profile_fullname;
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		$rules = array(
			array('username, password, email', 'required'),
			array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Tên nhập không đúng (phải từ 3 đến 20 kí tự).")),
			array('password', 'length', 'max'=>128, 'min' => 4,'message' => UserModule::t("Mật khẩu quá ngắn (ít nhất 3 kí tự).")),
			array('email', 'email'),
			array('username', 'unique', 'message' => UserModule::t("Tài khoản này đã tồn tại.")),
			array('email', 'unique', 'message' => UserModule::t("Thư điện tử này đã được đăng kí")),
			//array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' => UserModule::t("Retype Password is incorrect.")),
			array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Tài khoản không được có kí tự đăc biệt")),
			array('status', 'in', 'range'=>array(STATUS_NOACTIVE,STATUS_ACTIVE,self::STATUS_BANNED)),
			array('id, username, password, email, activkey, create_at, lastvisit_at, superuser, status', 'safe', 'on'=>'search'),
		);
		$old_password 	= $this->notsafe()->findByPk($this->id);
		$new_password	= (isset($_POST['SuperMod'])) ? $_POST['SuperMod']['password'] : null;
		if ($old_password == null || $old_password->password != $new_password) {
			array_push($rules, array('verifyPassword', 'required'));
			array_push($rules,array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' => UserModule::t("Mật khẩu nhập lại không đúng.")));
		}

		array_push($rules, array('profile_fullname', 'safe', 'on'=>'search'));


		return $rules;
	}

	/**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('username',$this->username,true);
        $criteria->compare('password',$this->password);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('activkey',$this->activkey);
        $criteria->compare('create_at',$this->create_at, true);
        $criteria->compare('lastvisit_at',$this->lastvisit_at, true);
        $criteria->compare('superuser',$this->superuser);
        $criteria->compare('status',$this->status);
        $criteria->addCondition('superuser = \'2\'');

        if($this->profile_fullname)
        {
        	$criteria->together  =  true;
            $criteria->with = array('profile');
            $criteria->compare('profile.fullname',$this->profile_fullname, true);
        }

        return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
        	'pagination'=>array(
				'pageSize'=>PAGE_SIZE,
			),
			'sort' =>array (
                'defaultOrder' => array('id'=> 'desc'),
				'attributes' => array(
					'profile_fullname' => array(
						'asc'=>'profile.fullname ASC',
						'desc'=>'profile.fullname DESC',
					),
					'username',
					'email',
					'create_at',
					'lastvisit_at'
				),
			)
		));
    }
}